package fr.charlott.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import fr.charlott.business.dto.ReunionDTO;
import fr.charlott.business.service.ReunionService;

@Component("listeReunionControler")
@Scope("view")
public class ListeReunionControler extends CharlottControler {

	private static final long serialVersionUID = 2465999191538831539L;

	private List<ReunionDTO> reunions = new ArrayList<ReunionDTO>();

	private ReunionDTO selectedReunion;
	
	private ReunionService reunionService;

	public ListeReunionControler() {
		super();
	}

	@Autowired(required = true)
	public ListeReunionControler(ReunionService reunionService) {
		this();
		
		this.reunionService = reunionService;

		// Initialisation des différentes Reunions
		reunions = reunionService.chargerToutesLesReunions();
	}

	// actions

	public String modifierReunion() {
		// Mettre la Reunion selectionnee dans la session pour le passage par redirection
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		externalContext.getSessionMap().put(NouvelleReunionControler.EDITED_REUNION, selectedReunion);

		return "nouvelleReunion";
	}
	
	public String supprimerReunion() {
		reunionService.supprimerReunion(selectedReunion);
		return "listeReunions";
	}

	// getters - setters

	public List<ReunionDTO> getReunions() {
		return reunions;
	}

	public ReunionDTO getSelectedReunion() {
		return selectedReunion;
	}

	public void setSelectedReunion(ReunionDTO selectedReunion) {
		this.selectedReunion = selectedReunion;
	}
}
